# [src/util/VoiceUtil.py]
from PySide6.QtCore import QObject
import pyttsx3

class VoiceUtil(QObject):
    _instance = None
    _initialized = False

    def __new__(cls, *args, **kwargs):
        if not cls._instance:
            cls._instance = super().__new__(cls)
        return cls._instance

    def __init__(self, parent=None):
        if not self._initialized:  # 防止重复初始化
            super().__init__(parent)
            self._init_engine()
            self._initialized = True

    def _init_engine(self):
        """初始化语音引擎"""
        self.engine = pyttsx3.init()
        self._is_speaking = False

    def speak(self, text):
        if self._is_speaking:
            self.engine.stop()
        self.engine.say(text)
        self.engine.runAndWait()
